﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.product
{
    public partial class product_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int company_id;
        protected int status;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.status = DTRequest.GetQueryInt("status");
            this.keywords = DTRequest.GetQueryString("keywords");
            this.company_id = DTRequest.GetInt("company_id",0);
            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                //BindDropDownList();
                //ChkAdminLevel("sys_channel_manage", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind("id>0" + CombSqlTxt(status, keywords, company_id), "id desc");
            }
        }

        #region DropDownList绑定=========================
        //private void BindDropDownList() {
        //    BLL.company bll = new BLL.company();
        //    this.ddlcompany.Items.Add(new ListItem("选择企业", ""));
        //    ddlcompany.DataSource = bll.GetList(999, " status=1", " group_id desc,sort_id desc,id desc");
        //    ddlcompany.DataTextField = "title";
        //    ddlcompany.DataValueField = "id";
        //    ddlcompany.DataBind();
        //}
        #endregion 
        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {

            this.page = DTRequest.GetQueryInt("page", 1);
            ddlStatus.SelectedValue = this.status.ToString();
            txtKeywords.Text = this.keywords;
            BLL.product bll = new BLL.product();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("product_list.aspx", "status={0}&keywords={1}&page={2}", this.status.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(int _status, string _keywords,int _company_id)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_status > 0)
            {
                strTemp.Append(" and status=" + _status);
            }
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords + "%' ");//or lianxiren like '%" + _keywords + "%'
                strTemp.Append(" or company_id in(select id from ch_company where title like  '%" + _keywords + "%' or mobile like '%" + _keywords + "%'))");
            }
            if (_company_id > 0) {
                strTemp.Append(" and company_id="+_company_id);
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("product_page_size", "DTcmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("product_list.aspx", "status={0}&keywords={1}", this.status.ToString(), txtKeywords.Text));
        }



        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("product_page_size", "DTcmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("product_list.aspx", "status={0}&keywords={1}", this.status.ToString(), this.keywords));
        }


        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //ChkAdminLevel("sys_channel_manage", DTEnums.ActionEnum.Delete.ToString());//检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.product bll = new BLL.product();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(3,id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("product_list.aspx", "status={0}&keywords={1}", this.status.ToString(), this.keywords), "parent.loadMenuTree");
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("product_list.aspx", "status={0}&keywords={1}", ddlStatus.SelectedValue, this.keywords));
        }

        //protected void ddlcompany_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    Response.Redirect(Utils.CombUrlTxt("product_list.aspx", "status={0}&company_id={1}", ddlStatus.SelectedValue,ddlcompany.SelectedValue));
        //}
    }
}